package com.muyan.common.bean;

import java.util.*;

/**
 * 参数构建实体
 *
 * @author zzl 2018/03/19
 */
@SuppressWarnings("unchecked")
public class Params extends HashMap<String, Object> {

    public static Params build() {
        return new Params();
    }

    public static Params build(Params params) {
        return new Params().putAll(params);
    }

    public static Params build(Map<String, Object> map) {
        Params params = new Params();
        params.putAll(map);
        return params;
    }

    /**
     * 快速构建参数列表
     */
    public static Params of(Object... value) {
        Params map = build();
        if (value == null || value.length % 2 != 0) {
            throw new IllegalArgumentException("参数必需成对传入");
        }
        for (int i = 0; i < value.length; i += 2) {
            map.put(value[i].toString(), value[i + 1]);
        }
        return map;
    }

    /**
     * 获取HttpServletRequest.getParameterMap提交的参数集合<br/>
     */
    public static Params getParams(Map<String, String[]> requestMap) {
        Params params = build();
        for (String key : requestMap.keySet()) {
            String[] values = requestMap.get(key);
            if (values == null) {
                continue;
            }
            if (values.length == 1) {
                params.put(key, values[0]);
            } else {
                params.put(key, values);
            }
        }
        return params;
    }

    public Params put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public Params putAll(Params params) {
        if (params != null) {
            super.putAll(params);
        }
        return this;
    }

    /**
     * 拼接成url字符串，默认是按照Key的字典顺序排序，形如：a=1&b=2
     */
    public String join() {
        return join(true);
    }

    /**
     * 拼接成url字符串，形如：a=1&b=2
     *
     * @param toSort 是否按照key的字典顺序排序
     */
    public String join(boolean toSort) {
        List<String> keys = new ArrayList<>(this.keySet());
        if (keys.size() == 0) return "";
        if (toSort) Collections.sort(keys);
        StringBuilder sb = new StringBuilder();
        for (String key : keys) {
            sb.append(key).append("=").append(this.get(key)).append("&");
        }
        return sb.deleteCharAt(sb.length() - 1).toString();
    }

    /**
     * 注意，非String对象会调用该对象的toString方法
     */
    public String getString(String key) {
        Object value = this.get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof String) {
            return (String) value;
        }
        return value.toString();
    }

    /**
     * 注意，实际类型不匹配会抛出异常
     */
    public <T> T getValue(String key) {
        return (T) this.get(key);
    }

    /**
     * 获取长整形数据
     * 可能会发生类型转换异常
     */
    public Long getLong(String key) {
        Object value = this.get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof String) {
            return Long.valueOf(value.toString());
        }
        return (Long) this.get(key);
    }

    /**
     * 获取整形数据
     * 可能会发生类型转换异常
     */
    public Integer getInteger(String key) {
        Object value = this.get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof String) {
            return Integer.valueOf(value.toString());
        }
        return (Integer) this.get(key);
    }

    /**
     * 获取Boolean类型数据
     * 可能会发生类型转换异常
     */
    public Boolean getBoolean(String key) {
        Object value = this.get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof String) {
            return Boolean.valueOf(value.toString());
        }
        return (Boolean) this.get(key);
    }

    /**
     * 获取Double类型数据
     * 可能会发生类型转换异常
     */
    public Double getDouble(String key) {
        Object value = this.get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof String) {
            return Double.valueOf(value.toString());
        }
        return (Double) this.get(key);
    }

    /**
     * 获取Float类型数据
     * 可能会发生类型转换异常
     */
    public Float getFloat(String key) {
        Object value = this.get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof String) {
            return Float.valueOf(value.toString());
        }
        return (Float) this.get(key);
    }

    /**
     * 获取Byte类型数据
     * 可能会发生类型转换异常
     */
    public Byte getByte(String key) {
        Object value = this.get(key);
        if (value == null) {
            return null;
        }
        if (value instanceof String) {
            return Byte.valueOf(value.toString());
        }
        return (Byte) this.get(key);
    }

}
